/***************************************************************************************************
  Copyright(C)   2011-9-13
  All rights reserved.
  File Name: qextserialthread.h

  Description:串口线程模块
  在线路中利用类Posix_QextSerialPort，每隔一段时间读取类Posix_QextSerialPort中是否有数据传送进来，
  然后再调用一个信号发送出来，从此省去了主线程中不断查询消息所耗费的时间

  ***************************************************************************************************/

#ifndef QEXTSERIALTHREAD_H
#define QEXTSERIALTHREAD_H

#include <QThread>
#include <QByteArray>
#include <QTimer>
#include <QDebug>

#include "serialport.h"

#ifdef _TTY_WIN_   //for win
#include "public/win_qextserialport.h"
#else
#include "public/posix_qextserialport.h"
#endif


class SerialPort;

extern Debug_Config_s g_stDebugConfig;

class QextSerialThread : public QThread
{
    Q_OBJECT
public:
    explicit QextSerialThread(QString port, SerialPort *parent = 0);


    bool isOpen() const;
    bool isReadable() const;
    bool isWritable() const;

    void setPort(const QString& port);
    void setReadTime(int timeSize);
    void setBaudRate(BaudRateType);
    void setStopBits(StopBitsType);
    void setDataBits(DataBitsType dataBits);
    void setParity(ParityType);
    void setFlowControl(FlowType);
    void setTimeout(long);


    bool openSeriPort(QIODevice::OpenMode openMode);
    void closeSeriPort();

    void setPortSettings(PortSettings portSettings);
    void setDtr(bool set=true);
    void setRts(bool set=true);


protected:
    void run();

signals:
    void receBytes(QByteArray receBytes);

private:

#ifdef _TTY_WIN_   //for win
    Win_QextSerialPort *serialPort;         //第三方写的串口类
#else
    Posix_QextSerialPort *serialPort;         //第三方写的串口类
#endif


    bool qextIsCreate;
    PortSettings mPortSettings;               //存贮串口设置的结构体，如数据位，检验位，停止位等
    QByteArray mReceBytes;                    //接收到的数据

    //读取串口数据定时器，因为Linux下类没有事件机制，所以只能在一定时间内自己读取串口的缓冲区，看有没数据发送过来
    QTimer  *readTimer;

    QString mPort;                          //串口名字
    QIODevice::OpenMode mOpenMode;          //打开方式，文件打开方式类同，可读可写等
    int mTimeSize;                          //读取串口缓冲区数据的时间

    bool isOpenDtr;
    bool isOpenRts;

public slots:
    void timeToRead();
    qint64 sentByteSlot(QByteArray sendBytes) const;

    void changeReceTimeAtRunning(int timeSize);





};

#endif // QEXTSERIALTHREAD_H
